package com.googlecode.chigs.web.config.configrevision;

import org.apache.wicket.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.extensions.wizard.WizardModel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.request.target.basic.RedirectRequestTarget;
import org.apache.wicket.spring.injection.annot.SpringBean;

import brix.BrixNodeModel;
import brix.web.nodepage.BrixNodeWebPage;
import brix.web.nodepage.BrixPageParameters;

import com.googlecode.chigs.domain.ConfigRevision;
import com.googlecode.chigs.domain.Configuration;
import com.googlecode.chigs.domain.Product;
import com.googlecode.chigs.repository.ChigsFacade;
import com.googlecode.chigs.web.ChigsApplication;
import com.googlecode.chigs.web.config.configpair.EditOrCreateConfigPairStep;
import com.googlecode.chigs.web.config.configuration.EditOrCreateConfigurationStep;
import com.googlecode.chigs.web.config.module.EditOrCreateModuleStep;
import com.googlecode.chigscommons.repository.GenericDomainService;
import com.googlecode.chigscommons.web.WicketWebSession;
import com.googlecode.chigscommons.web.common.wizard.DefaultWizard;

@AuthorizeInstantiation("ROLE_USER")
public class EditOrCreateConfigRevisionWizard extends DefaultWizard {
	@SpringBean
	private ChigsFacade chigs;

	@SpringBean
	private GenericDomainService genericDomainService;

	private final ConfigRevision configRevision;
	private final Product product;
	private Configuration configuration;

	public EditOrCreateConfigRevisionWizard(String id, Product product) {
		this(id, null, product);
	}

	public EditOrCreateConfigRevisionWizard(String id, Configuration configuration, Product product) {
		super(id);
		
		this.product = product;
		
		if (configuration == null) {
			this.configRevision = new ConfigRevision();
		} else {
			this.configRevision = chigs.loadLatestConfigRevision(configuration);
		}
		
		this.configRevision.setUser(((WicketWebSession) WicketWebSession.get()).getUser());
		init();
	}

	public EditOrCreateConfigRevisionWizard(String id, Product product, Configuration configuration, ConfigRevision configRevision) {
		super(id, false);
		
		this.product = product;
		this.configuration = configuration;
		
		this.configRevision = genericDomainService.load(ConfigRevision.class, configRevision.getId());
		init();
	}

	private void init() {
		setDefaultModel(new CompoundPropertyModel<EditOrCreateConfigRevisionWizard>(this));
		
		WizardModel model = new WizardModel();
		model.add(new EditOrCreateConfigurationStep(this.configRevision, this.product, isEditable()));
		model.add(new EditOrCreateModuleStep(this.configRevision, this.product, isEditable()));
		model.add(new EditOrCreateConfigPairStep(this.configRevision, isEditable()));
		init(model);
	}
	
	@Override
	public void onCancel() {
		returnToManagement();
	}
	
	@Override
	public void onFinish() {
		try {
			if (isEditable()) {
				chigs.saveConfigRevision(configRevision);
				getSession().info(new StringResourceModel("save.success", EditOrCreateConfigRevisionWizard.this, null).getString());
			}
			returnToManagement();
		} catch (Exception e) {
			error(e.getMessage() != null ? e.getMessage() : e.toString());
		}
	}

	private void returnToManagement() {
		if (isEditable()) {
			BrixPageParameters params = new BrixPageParameters();
			params.addQueryParam("product", product.getId().toString());
			BrixNodeModel model = ((ChigsApplication) getApplication()).getI18nBrixLink(getSession(), "/configuration/configurations.html");
			getRequestCycle().setRequestTarget(new RedirectRequestTarget(params.urlFor(new BrixNodeWebPage(model))));
		} else {
			BrixPageParameters params = new BrixPageParameters();
			params.addQueryParam("product", product.getId().toString());
			params.addQueryParam("configuration", configuration.getId().toString());
			BrixNodeModel model = ((ChigsApplication) getApplication()).getI18nBrixLink(getSession(), "/configuration/revisions.html");
			getRequestCycle().setRequestTarget(new RedirectRequestTarget(params.urlFor(new BrixNodeWebPage(model))));
		}
	}
}

